<?php
	require_once $_SERVER['DOCUMENT_ROOT'] . '/deviantpic/inc/database.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/deviantpic/inc/profile.php';

	//Ensure it's the logged in user
	if ($user['id'] != $_SESSION['user']) {
		header("Location:http://" . $_SERVER['HTTP_HOST'] . '/deviantpic/index.php');
		die();
	}

	//Get gender
	$genders = array();
	$result = mysql_query("SELECT * FROM genders");
	$row	= mysql_fetch_array($result);
	while ($row) {
		$genders[]	= $row;
		$row	= mysql_fetch_array($result);
	}

	//Get privileges
	$privileges = array();
	$result = mysql_query("SELECT * FROM wall_privileges");
	$row	= mysql_fetch_array($result);
	while ($row) {
		$privileges[]	= $row;
		$row			= mysql_fetch_array($result);
	}

	//Get avatars
	$avatars = array();
	$result = mysql_query("SELECT photo_id FROM tags WHERE user_id='" . $user['id'] . "' AND is_owner='1'");
	$row	= mysql_fetch_array($result);
	while ($row) {
		$avatar_result	= mysql_query("SELECT * FROM photos WHERE id='" . $row['photo_id'] . "'");
		$avatars[]		= mysql_fetch_array($avatar_result);

		//Next
		$row = mysql_fetch_array($result);
	}


	require_once $_SERVER['DOCUMENT_ROOT'] . '/deviantpic/inc/doctype.php';
?>

    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <title>deviantPIC - Edit profile</title>
    </head>
    <body>
		<!-- Header -->
		<?php require $_SERVER['DOCUMENT_ROOT'] . '/deviantpic/inc/header.php'; ?>

		<!-- Content -->
		<div id="body">

			<h2>Edit profile</h2>

			<!-- Pictures -->
			<form action=<?php echo '"index.php?id=' . $_SESSION['user'] . '"'; ?> method="post"> <p>
				<!-- Avatar -->
				<label for="input_avatar">Avatar: </label>
				<select name="avatar" id="input_avatar">
					<option value="-1">Default</option>
					<?php
						foreach ($avatars as $avatar) {
					?>
					<option value=<?php echo '"' . $avatar['id'] . '"' ?>><?php echo $avatar['title']; ?></option>
					<?php
						}
					?>
				</select> <br />

				<!-- Password input -->
				<label for="input_password" >Password: </label>
				<input type="password"
					   id="input_password"
					   name="password"
				/> <span id="result_password"></span> <br />

				<!-- Password confirmation -->
				<label for="confirm_password" >Confirm password: </label>
				<input type="password"
					   id="confirm_password"
				/> <span id="result_confirm"></span> <br />

				<!-- E-Mail input -->
				<label for="input_email">E-Mail: </label>
				<input type="text"
					   id="input_email"
					   name="email"
					   <?php
							echo "value='" . $user['email'] . "'";
					   ?>
				/> <span id="result_email"></span> <br />

				<!-- Name input -->
				<label for="input_name">Real name: </label>
				<input type="text"
					   id="input_name"
					   name="name"
					   <?php
							echo "value='" . $user['realname'] . "'";
					   ?>
				/> <span id="result_name"></span> <br />

				<!-- Date input -->
				<label for="input_birth">Birth date: </label>
				<input type="text"
					   id="input_birth"
					   name="birth"
					   <?php
							echo "value='" . $user['birth'] . "'";
					   ?>
				/> <span id="result_birth"></span> <br />

				<!-- Gender -->
				<label for="input_gender">Gender: </label>
				<select name="gender" id="input_gender">
					<?php
						foreach ($genders as $gender) {
							$selected = "";
							if ($gender['id'] == $user['gender']) $selected = " selected='selected'";

							echo '<option value="' . $gender['id'] . '"' . $selected . '>' . $gender['name'] . '</option>';
						}
					?>
				</select> <br />

				<!-- Location input -->
				<label for="input_location">Location: </label>
				<input type="text"
					   id="input_location"
					   name="location"
					   <?php
							echo "value='" . $user['location'] . "'";
					   ?>
				/> <span id="result_location"></span> <br />

				<!-- Wall -->
				<label for="input_wall-privilege">Who can see your wall: </label>
				<select name="privilege" id="input_wall-privilege">
					<?php
						foreach ($privileges as $privilege) {
							$selected = "";
							if ($privilege['id'] == $user['wall_privilege']) $selected = " selected='selected'";

							echo '<option value="' . $privilege['id'] . '"' . $selected . '>' . $privilege['name'] . '</option>';
						}
					?>
				</select> <br />

				<!-- Button -->
				<input type="submit" name="edit" value="Update" />
			</p></form>				
		</div>

		<!-- Footer -->
		<?php require $_SERVER['DOCUMENT_ROOT'] . '/deviantpic/inc/footer.php'; ?>
    </body>
</html>